package org.roger.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class CoordinateUtil {
	
	public static void getCoordinate(String addr){
		String address = null;
//		try {  
//            address = java.net.URLEncoder.encode(addr,"UTF-8");  
//        } catch (UnsupportedEncodingException e1) {  
//            e1.printStackTrace();  
//        }; 
        String output = "json";  
        String key = "abc";
        String city = "北京市";
        String url = String.format("http://api.map.baidu.com/geocoder?address=%s&output=%s&key=%s&city=%s", addr, output, key,city);
        URL myURL = null;
        URLConnection httpsConn = null;
        //进行转码  
        try {  
            myURL = new URL(url);  
        } catch (MalformedURLException e) {  
              
        }
        
        try {
        	httpsConn = (URLConnection) myURL.openConnection();
        	if (httpsConn != null) { 
        		InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream(), "UTF-8");
        		BufferedReader br = new BufferedReader(insr);
        		String data = null;
        		while((data = br.readLine()) != null) {  
                   System.out.println(data);
                }  
                insr.close();
        	}
        } catch (IOException e) {  
            e.printStackTrace();
        }
	}
	
	public static void main(String[] args) {
		getCoordinate("北京市海淀区上地十街18号");
	}

}
